package com.lzj.hbase2hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.IOException;

public class PhoneJob {

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        //获取配置文件
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("mapreduce.framework.name", "local");
        //创建任务对象
        Job job = Job.getInstance(configuration, "hbase2hdfs-" + System.currentTimeMillis());

        //设置当前任务的主类
        job.setJarByClass(PhoneJob.class);

        //设置任务的其他信息
        job.setNumReduceTasks(2);

        //[138,139)
        Scan scan = new Scan();
        scan.withStartRow("138".getBytes());
        scan.withStopRow("139".getBytes());

        //设置数据的map
        TableMapReduceUtil.initTableMapperJob("phone", scan, PhoneMapper.class, Text.class, IntWritable.class, job);

        //设置Reduce的处理类
        FileOutputFormat.setOutputPath(job, new Path("/lzj/result/hbase2hdfs" + System.currentTimeMillis()));
        job.setReducerClass(PhoneReducer.class);
        //提交任务
        job.waitForCompletion(true);
    }

}